<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_dataFlowService"></title>
    <script type="text/javascript" src="../js/include-web.js"></script>
    <style>
        .mapboxgl-popup {
            max-width: 200px;
        }
    </style>
</head>

<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
    <div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
    <script type="text/javascript" src="../../dist/mapboxgl/include-mapboxgl.js"></script>
    <script>
        var host = (window.isLocal ? window.server : "http://iclsvr.supermap.io"),
            tileUrl = host +
            '/iserver/services/map-china400/rest/maps/China/zxyTileImage.png?prjCoordSys={"epsgCode":3857}&z={z}&x={x}&y={y}',
            urlQuery = host + "/iserver/services/map-china400/rest/maps/China_4326";

        wsHost = "ws:\//" + (window.isLocal ? document.location.hostname + ":8800" : "iclsvrws.supermap.io");
        mapboxgl.accessToken =
            'pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4M29iazA2Z2gycXA4N2pmbDZmangifQ.-g_vE53SD2WrJ6tFX7QHmA';

        var urlDataFlow = wsHost + "/iserver/services/dataflowTest/dataflow";
        var attribution = "<a href='https://www.mapbox.com/about/maps/' target='_blank'>© Mapbox </a>" +
            " with <span>© <a href='http://iclient.supermap.io' target='_blank'>SuperMap iClient</a> | </span>" +
            " Map Data <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> ";
        var dataFlowService;
        var map = new mapboxgl.Map({
            container: 'map',
            style: {
                "version": 8,
                "sources": {
                    "raster-tiles": {
                        "attribution": attribution,
                        "type": "raster",
                        "tiles": [tileUrl],
                        "tileSize": 256,
                    },
                },
                "layers": [{
                    "id": "simple-tiles",
                    "type": "raster",
                    "source": "raster-tiles",
                    "minzoom": 0,
                    "maxzoom": 18
                }],
            },
            center: [116.443571, 39.887549],
            maxZoom: 18,
            zoom: 10
        });
        map.addControl(new mapboxgl.supermap.LogoControl(), 'bottom-right');
        map.addControl(new mapboxgl.NavigationControl(), 'top-left');

        SuperMap.SecurityManager.registerToken(urlDataFlow, window.exampleToken);
        var popup = new mapboxgl.Popup();

        map.on('load', function () {
            var options = {
                ws: urlDataFlow
            };
            var dataFlowSubscribe = new mapboxgl.supermap.DataFlowService(options.ws, {
                geometry: options.geometry,
                prjCoordSys: options.prjCoordSys,
                excludeField: options.excludeField
            }).initSubscribe();
            dataFlowSubscribe.on('messageSucceeded', function (msg) {
                popup.remove();
                addLayer(msg);
            });
            query();
        });

        //模拟实时数据
        //查询一个线数据，每两秒将一个点通过dataFlowService广播给iSevrer的dataflow服务
        function query() {
            var param = new SuperMap.QueryBySQLParameters({
                queryParams: {
                    name: "Main_Road_L@China#1",
                    attributeFilter: "SMID = 1755"
                }
            });
            queryService = new mapboxgl.supermap.QueryService(urlQuery).queryBySQL(param, function (serviceResult) {
                featureResult = serviceResult;
                dataFlowBroadcast = new mapboxgl.supermap.DataFlowService(urlDataFlow).initBroadcast();
                dataFlowBroadcast.on('broadcastSocketConnected', function (e) {
                    timer = window.setInterval("broadcast()", 2000);
                });

            });
        }

        var count = 200;

        function broadcast() {

            if (count >= featureResult.result.recordsets[0].features.features[0].geometry.coordinates.length) {
                window.clearInterval(timer);
                return;
            }
            var point = featureResult.result.recordsets[0].features.features[0].geometry.coordinates[count];
            var feature = {
                geometry: {
                    coordinates: [point[0], point[1]],
                    type: "Point"
                },
                type: "Feature",
                properties: {
                    id: 1,
                    time: new Date()
                }
            };
            dataFlowBroadcast.broadcast(feature);
            count += 3;
        }

        function addLayer(msg) {
            if (!msg.featureResult) {
                return;
            }
            var feature = msg.featureResult;
            var coord = feature.geometry.coordinates;
            var data = {
                geometry: {
                    type: 'Point',
                    coordinates: coord,
                },
                type: "Feature"
            };

            if (!map.getSource('location')) {
                map.addSource('location', {
                    'type': 'geojson',
                    'data': data
                });
                map.addLayer({
                    "id": "point",
                    "type": "circle",
                    "paint": {
                        "circle-radius": 6,
                        "circle-color": 'red',
                    },
                    "source": 'location'
                });
            }

            map.getSource('location').setData(data);

            popup.setLngLat(coord)
                .setHTML(feature.properties.time)
                .addTo(map);
        }
    </script>

</body>

</html>